<template>
  <div class="dashboard-container">
    <div class="app-container">
      <page-tools>
        <el-button slot="after" type="primary" size="small" @click="addPermission(1, 0)">添加权限</el-button>
      </page-tools>

      <el-card>
        <!-- 放置权限列表 -->
        <el-table border :data="list" row-key="id">
          <el-table-column prop="name" label="名称" />
          <el-table-column prop="code" align="center" label="标识" />
          <el-table-column prop="description" align="center" label="描述" />
          <el-table-column align="center" label="操作">
            <template v-slot="{ row }">
              <el-button v-if="row.type===1" type="text" @click="addPermission(2,row.id)">添加</el-button>
              <el-button type="text" @click="editPermission(row.id)">编辑</el-button>
              <el-button type="text" @click="delPermission(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!-- 新增编辑弹层 -->
      <el-dialog :title="cTitle" :visible="showDialog" @close="btnCancel">
        <el-form ref="perForm" :model="formData" :rules="rules" label-width="120px">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="formData.name" style="width:90%" />
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="formData.code" style="width:90%" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="formData.description" style="width:90%" />
          </el-form-item>
          <el-form-item label="开启">
            <el-switch
              v-model="formData.enVisible"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" type="primary" @click="btnOK">确定</el-button>
            <el-button size="small" @click="btnCancel">取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getPermissionList, delPermission, addPermission, getPermissionDetail, updatePermission } from '@/api/permission'
import { transListToTreeData } from '@/utils'
export default {
  data() {
    return {
      list: [], // 权限的列表
      showDialog: false, // 控制编辑新增弹层
      formData: {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型
        pid: '', // 因为做的是树   需要知道添加到那个节点下
        enVisible: '0' // 开启
      },
      rules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    cTitle() {
      return this.formData.id ? '编辑权限' : '新增权限'
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      // 将数据转化成了
      this.list = transListToTreeData(await getPermissionList(), '0')
    },
    delPermission(id) {
      this.$confirm('确定删除吗').then(() => {
        return delPermission(id)
      }).then(() => {
        this.$message.success('删除成功')
        this.getPermissionList()
      })
    },
    addPermission(type, pid) {
      // 访问权限type= 1 按钮操作的type= 2
      // pid表示当前的数据的父节点标识
      // 记录当前的添加的类型和夫标识
      this.formData.type = type
      this.formData.pid = pid
      this.showDialog = true
    },
    // 点击编辑
    async editPermission(id) {
      this.formData = await getPermissionDetail(id)
      this.showDialog = true
    },
    // 点击确定
    btnOK() {
      this.$refs.perForm.validate().then(() => {
        // 校验成功
        if (this.formData.id) {
          return updatePermission(this.formData)
        }
        return addPermission(this.formData)
      }).then(() => {
        this.$message.success('新增成功')
        this.getPermissionList()
        this.showDialog = false
      })
    },
    // 点击取消
    btnCancel() {
      this.formData = {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型
        pid: '', // 因为做的是树   需要知道添加到那个节点下
        enVisible: '0' // 开启
      }
      this.$refs.perForm.resetFields()// 移除校验
      this.showDialog = false
    }
  }
}
</script>

<style>

</style>
